<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <svg-icon icon-class="skill" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item>添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree :data="list" :default-expand-all="true">
          <!-- 作用域插槽 data拿到的是每一个子节点的对象 -->
          <!--
      #default === slot-scope
      什么时候用到作用域插槽？父组件中如果想使用子组件中的数据进行自定义内容的渲染 (类似于table 单元格数据渲染)
    -->
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children && data.children.length===0 " @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <!-- dialog对话框组件 -->
      <el-dialog
        title="提示"
        :visible.sync="dialogVisible"
        :close-on-press-escape="false"
        :close-on-click-modal="false"
      >
        <!-- 子组件：一个表单 -->
        <departmentDialog v-if="dialogVisible" :id="DadId" :is-edit="isEdit" :origin-list="originList" @success="hsuccess" @close="dialogVisible = false" />
      </el-dialog>

    </div>
  </div>
</template>

<script>
import departmentDialog from './departmentDialog.vue'
import { delDepartmentsById, getDepartments } from '@/api/departments'
import { processingDepartments } from '@/utils'

export default {
  components: {
    departmentDialog
  },
  data() {
    return {
      // 依赖一份树形数据
      list: [],
      // 控制对话框的显示和隐藏
      dialogVisible: false,
      // 控制提交的是is
      isEdit: false,
      DadId: '',
      originList: []
    }
  },
  created() {
    // 界面刷新渲染
    this.loadDepartments()
  },
  methods: {
    // 刷新渲染
    async loadDepartments() {
      const res = await getDepartments()
      console.log('获取组织架构数据成功', res)
      const arr = res.data.data.depts
      this.originList = arr.map(item => {
        const { name, code, id, pid } = item
        return { name, code, id, pid }
      })
      this.list = processingDepartments(arr)
    },
    // 添加数据
    hAdd(id) {
      this.dialogVisible = true
      this.isEdit = false
      this.DadId = id
    },
    // 修改数据
    hEdit(id) {
      this.dialogVisible = true
      this.isEdit = true
      this.DadId = id
    },
    // 删除数据
    hDel(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // 发删除请求
        await delDepartmentsById(id)
        // 刷新渲染
        this.loadDepartments()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 成功关闭对话框和刷新界面
    hsuccess() {
      this.dialogVisible = false
      this.loadDepartments()
    }
  }
}
</script>
